import { createStore } from 'vuex'

export default createStore({
  state: {
    count: 100,
    num: 10,
    name:'',
    data:[],
    shopping:[],
    nub:0,
    nex:'',
  },
  mutations: {
    increment (state,sum) {
        state.name = sum
    },
    // 购物车数据
    shopping(state,sum){
      state.data = sum
    },
    // 购物车点击添加事件
    shoppingadd(state,sum){
      let s = sum;
      let flog = false;

      state.shopping.map(item => {
        if(item.id == s.id){
          flog = true
        }else{
          flog = false
        }
      })

      if(flog == false){
        state.shopping = [...state.shopping,sum]
      }
      console.log(flog)
    },

    // 全选购物车
    SelectAllCheck(state,sum){
      if(state.shopping.length == 0){
        return
      }else{
        state.shopping.map((item,index) => {
          state.shopping[index].size = sum
        })
        console.log(state.shopping[0].size)
        state.shopping.map(item => {
          if(item.size){
            var i = state.nub += item.age;
            state.nub = i
          }else{
            state.nub = 0
          }
        })
      }
    },

    // 删除购物车
    DeleteShoppingCart(state,num){
      console.log(state.nex)
      var x =state.nub -= state.shopping[state.nex].age;
      if(state.nub <= 0){
        state.nub = 0
      }
      state.nub = x;
      state.shopping = state.shopping.filter(item => {
        return item.id != num
      })
    },
    SelectAlltowc(state,num){
      state.nex = num;
      console.log(state.nex)
    },

    // 反选
    SelectAlltow(state,num){
      state.shopping[state.nex].size = num
      state.shopping.map(item => {
        if(item.size){
          var i = state.nub += item.age;
          state.nub = i
        }else{
          state.nub = 0
        }
      })
    },

    // 结算
    delete(state){
      state.shopping = [];
      state.nub = 0
    }
  },
  actions: {
  },
  modules: {
  }
})
